package com.vict.config;

import com.vict.config.h2.H2DataSourceConfig;
import com.vict.job.Job;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;


@Slf4j
@Component
public class Init {


    @Autowired
    private SettingConfig settingConfig;

    @Autowired
    private Job job;

    @Autowired
    private H2DataSourceConfig h2DataSourceConfig;


    @Value("${server.port}")
    private String serverPort;

    @Value("${spring.datasource.url}")
    private String h2Url;

    @Value("${spring.datasource.username}")
    private String h2Username;

    @Value("${spring.datasource.password}")
    private String h2Password;


    @Value("${my-version}")
    private String myVersion;

    /** 程序默认初始化标记 */
    public static boolean isInit = false;

    public void init(){
        try{
            log.info("初始化开始");
            log.info("当前程序版本: {}", myVersion);

            File file = new File("version-" + this.myVersion);
            if(file.exists()){ // 存在标记 表示已经初始化过
                isInit = true;
            }

            settingConfig.init();

            h2DataSourceConfig.init();

            log.info("启动端口:{}", this.serverPort);

            log.info("h2数据库访问地址: http://127.0.0.1:{}/h2-console", this.serverPort);
            log.info("h2数据库访问JDBC_URL: {}", this.h2Url);
            log.info("h2数据库访问账号: {}", this.h2Username);
            log.info("h2数据库访问密码: {}", this.h2Password);

            // 启动主循环定时任务
            job.init();

            if(isInit == false){
                file.createNewFile();
                isInit = true;
            }

            log.info("初始化完成");
        }catch(Exception e){
            log.error("初始化异常", e);
        }
    }
}
